/*
 * OthersView.java
 *
 * Created on August 23, 2006, 12:40 AM
 */
package com.floreantpos.ui.views.order;

import com.floreantpos.POSConstants;
import com.floreantpos.config.PrintConfig;
import com.floreantpos.main.Application;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketItem;
import com.floreantpos.model.dao.TicketDAO;
import com.floreantpos.print.PosPrintService;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.dialog.MiscTicketItemDialog;
import com.floreantpos.ui.dialog.NotesDialog;
import com.floreantpos.ui.dialog.NumberSelectionDialog2;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.ui.dialog.TicketDetailDialog;
import com.floreantpos.ui.views.CookingInstructionView;

/**
 *
 * @author MShahriar
 */
public class OthersView extends TransparentPanel {

  private Ticket currentTicket;

  /**
   * Creates new form OthersView
   */
  public OthersView() {
    initComponents();
  }

  private void initComponents() {

    transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
    btnOrderInfo = new com.floreantpos.swing.PosButton();
    btnMisc = new com.floreantpos.swing.PosButton();
    btnPrintReceipt = new com.floreantpos.swing.PosButton();
    transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
    btnCustomerNumber = new com.floreantpos.swing.PosButton();
    btnTableNumber = new com.floreantpos.swing.PosButton();
    btnCookingInstruction = new com.floreantpos.swing.PosButton();

    setBorder(javax.swing.BorderFactory.createTitledBorder(null, com.floreantpos.POSConstants.OTHERS, javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
    setPreferredSize(new java.awt.Dimension(120, 140));
    setLayout(new java.awt.GridLayout(0, 1, 5, 5));

    transparentPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    transparentPanel2.setPreferredSize(new java.awt.Dimension(140, 50));
    transparentPanel2.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

    btnOrderInfo.setText(com.floreantpos.POSConstants.ORDER_INFO);
    btnOrderInfo.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnOrderInfoActionPerformed(evt);
      }
    });
    transparentPanel2.add(btnOrderInfo);

    btnMisc.setText(com.floreantpos.POSConstants.MISC);
    btnMisc.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doInsertMisc(evt);
      }
    });
    transparentPanel2.add(btnMisc);

    btnCookingInstruction.setText("<html><body><center>" + POSConstants.CAPITAL_COOKING_INSTRUCTIONS + "</center></body></html>");
    btnCookingInstruction.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCookingInstructionActionPerformed(evt);
      }
    });
    transparentPanel2.add(btnCookingInstruction);

    add(transparentPanel2);

    transparentPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    transparentPanel1.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

    btnCustomerNumber.setText(com.floreantpos.POSConstants.CUSTOMER);
    btnCustomerNumber.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCustomerNumberActionPerformed(evt);
      }
    });
    transparentPanel1.add(btnCustomerNumber);

    btnTableNumber.setText(com.floreantpos.POSConstants.TABLE);
    btnTableNumber.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnTableNumberActionPerformed(evt);
      }
    });
    transparentPanel1.add(btnTableNumber);

    btnPrintReceipt.setText(com.floreantpos.POSConstants.PRINT_RECEIPT);
    btnPrintReceipt.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnPrintReceiptActionPerformed(evt);
      }
    });
    transparentPanel1.add(btnPrintReceipt);

    add(transparentPanel1);
  }

  private void btnCookingInstructionActionPerformed(java.awt.event.ActionEvent evt) {
    CookingInstructionView cookingInstructionView = new CookingInstructionView(Application.getPosWindow(), true);
    cookingInstructionView.setTitle(com.floreantpos.POSConstants.COOKING_INSTRUCTIONS);
    cookingInstructionView.pack();
    cookingInstructionView.setTicket(getCurrentTicket());
    cookingInstructionView.open();
  }

  private void doInsertMisc(java.awt.event.ActionEvent evt) {
    MiscTicketItemDialog dialog = new MiscTicketItemDialog(Application.getPosWindow(), true);
    dialog.open();
    if (!dialog.isCanceled()) {
      TicketItem ticketItem = dialog.getTicketItem();
      RootView.getInstance().getOrderView().getTicketView().addTicketItem(ticketItem);
    }
  }

  private void btnPrintReceiptActionPerformed(java.awt.event.ActionEvent evt) {
    try {
      if (saveOrder()) {
        if (PrintConfig.isPrintKitchenManual()) {
          PosPrintService.printToKitchen(getCurrentTicket());
          RootView.getInstance().getOrderView().getTicketView().updateAllView();
        } else {

          if (!getCurrentTicket().isPrintedTicket()) {
            getCurrentTicket().setPrintedTicket(true);
          }

          PosPrintService.printTicket(getCurrentTicket());
        }
      }
    } catch (Exception e) {
      POSMessageDialog.showError(Application.getPosWindow(), e.getMessage(), e);
    }
  }

  private Boolean saveOrder() {
    if (getCurrentTicket().getTicketItems() == null || getCurrentTicket().getTicketItems().isEmpty()) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.TICKET_IS_EMPTY_);
      return false;
    }

    OrderController.saveOrder(getCurrentTicket());
    return true;
  }

  private void btnOrderInfoActionPerformed(java.awt.event.ActionEvent evt) {
    if (saveOrder()) {
      TicketDetailDialog dialog = new TicketDetailDialog(Application.getPosWindow(), true);
      dialog.setTicket(getCurrentTicket());
      dialog.open();

      if (!dialog.isCanceled()) {
        OrderView.getInstance().getTicketView().updateView();
      }
    }
  }

  private void btnCustomerNumberActionPerformed(java.awt.event.ActionEvent evt) {
    updateGuestNumber();
  }

  private void updateGuestNumber() {
    Ticket thisTicket = getCurrentTicket();
    int guestNumber = thisTicket.getNumberOfGuests();

    NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
    dialog.setTitle(com.floreantpos.POSConstants.NUMBER_OF_GUESTS);
    dialog.setValue(guestNumber);
    dialog.pack();
    dialog.open();

    if (dialog.isCanceled()) {
      return;
    }

    guestNumber = (int) dialog.getValue();
    if (guestNumber == 0) {
      POSMessageDialog.showError(Application.getPosWindow(), com.floreantpos.POSConstants.GUEST_NUMBER_CANNOT_BE_0);
      return;
    }

    thisTicket.setNumberOfGuests(guestNumber);
    updateView();

  }

  private void btnTableNumberActionPerformed(java.awt.event.ActionEvent evt) {
    updateTableNumber();
  }

  private void updateTableNumber() {
    Ticket thisTicket = getCurrentTicket();

    if (Application.isSaveGuestName()) {

      String guestName = thisTicket.getGuest();

      NotesDialog guestNameDialog = new NotesDialog(Application.getPosWindow(), true);
      guestNameDialog.setTitle(com.floreantpos.POSConstants.CUSTOMER_NAME);
      guestNameDialog.setNote(guestName);
      guestNameDialog.open();

      if (guestNameDialog.isCanceled()) {
        return;
      }

      guestName = guestNameDialog.getNote();

      if (guestName.isEmpty()) {
        POSMessageDialog.showError(Application.getPosWindow(), com.floreantpos.POSConstants.CUSTOMER_NAME_REQUIRED);
        return;
      }

      TicketDAO dao = TicketDAO.getInstance();
      Ticket otherTicket = dao.findTicketByGuestName(guestName);

      if (otherTicket != null && !otherTicket.equals(thisTicket)) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.ALREADY_EXISTS_CUSTOMER_NAME);
        return;
      }

      thisTicket.setGuest(guestName);

    } else if (Application.isAutoGenTicket()) {

      POSMessageDialog.showError(com.floreantpos.POSConstants.CANNOT_CHANGE_THE_PASSWORD);
      return;

    } else {

      int tableNumber = thisTicket.getTableNumber();

      NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
      dialog.setTitle(com.floreantpos.POSConstants.TABLE_NUMBER);
      dialog.setValue(tableNumber);
      dialog.pack();
      dialog.open();

      if (dialog.isCanceled()) {
        return;
      }

      tableNumber = (int) dialog.getValue();
      if (tableNumber == 0) {
        POSMessageDialog.showError(Application.getPosWindow(), com.floreantpos.POSConstants.TABLE_NUMBER_CANNOT_BE_0);
        return;
      }

      TicketDAO dao = TicketDAO.getInstance();
      Ticket otherTicket = dao.findTicketByTableNumber(tableNumber);

      if (otherTicket != null && !otherTicket.equals(thisTicket)) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.TABLE_OCCUPIED);
        return;
      }

      thisTicket.setTableNumber(tableNumber);
      thisTicket.setGuest(String.valueOf(tableNumber));

    }

    updateView();
  }

  private com.floreantpos.swing.PosButton btnCookingInstruction;
  private com.floreantpos.swing.PosButton btnCustomerNumber;
  private com.floreantpos.swing.PosButton btnMisc;
  private com.floreantpos.swing.PosButton btnOrderInfo;
  private com.floreantpos.swing.PosButton btnPrintReceipt;
  private com.floreantpos.swing.PosButton btnTableNumber;
  private com.floreantpos.swing.TransparentPanel transparentPanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel2;

  public void updateView() {
    if (currentTicket != null) {
      if (Application.isTestSystem()) {
        btnMisc.setEnabled(true);
      } else {
        btnMisc.setEnabled(false);
      }
      
      if (currentTicket.getTableNumber() == Ticket.TAKE_OUT) {
        btnCustomerNumber.setText("");
        btnTableNumber.setText("");

        btnCustomerNumber.setEnabled(false);
        btnTableNumber.setEnabled(false);
      } else {

        if (Application.isSaveGuestsPerTable()) {
          btnCustomerNumber.setEnabled(true);
          btnCustomerNumber.setText(POSConstants.GUEST + "s: " + currentTicket.getNumberOfGuests());
        } else {
          btnCustomerNumber.setEnabled(false);
          btnCustomerNumber.setText("");
        }

        btnTableNumber.setEnabled(true);

        if (Application.isSaveGuestName()) {
          btnTableNumber.setText(Application.getControlTypeIdentifier());
        } else {
          btnTableNumber.setText(Application.getControlTypeIdentifier() + ": " + currentTicket.getTableNumber());
        }

      }

      if (PrintConfig.isPrintReceiptEnabled()) {
        btnPrintReceipt.setText(com.floreantpos.POSConstants.PRINT_RECEIPT);
        btnPrintReceipt.setEnabled(true);
      } else {
        btnPrintReceipt.setText("");
        btnPrintReceipt.setEnabled(false);
      }

    }
  }

  public Ticket getCurrentTicket() {
    return currentTicket;
  }

  public void setCurrentTicket(Ticket currentTicket) {
    this.currentTicket = currentTicket;
    updateView();
  }
}
